﻿//兼容commonjs插件寫法 後期可以改進為 原型鏈方法
;
(function() {
    var mscrollList = function() {
        //初始化默认值
        this.params = {
            duration: 100,
            wrap_p: 'tab-wrap',
            wrap_1: 'tab1',
            wrap_2: 'tab2',
            warp_l: 'item-h',
            height: 30,
            Interval: false
        }
        if (arguments.length == 1) {
            for (var key in arguments[0]) {
                this.params[key] = arguments[0][key];
            }
        } else {
            this.params[arguments[0]] = arguments[1];
        };
        this.init()
    };
    mscrollList.prototype = {
        init: function() {
            this.requestAnimationFrame()
            this.evnets()
        },
        requestAnimationFrame: function() {
            for (var g = 0, h = ["ms", "moz", "webkit", "o"], i = 0; i < h.length && !window.requestAnimationFrame; ++i) {
                window.requestAnimationFrame = window[h[i] + "RequestAnimationFrame"],
                    window.cancelAnimationFrame = window[h[i] + "CancelAnimationFrame"] || window[h[i] + "CancelRequestAnimationFrame"]
            }
            window.requestAnimationFrame || (window.requestAnimationFrame = function(l, p) {
                    var k = (new Date).getTime(),
                        m = Math.max(0, 16 - (k - g)),
                        j = window.setTimeout(function() {
                            l(k + m)
                        }, m);
                    return g = k + m,
                        j
                }),
                window.cancelAnimationFrame || (window.cancelAnimationFrame = function(e) {
                    clearTimeout(e)
                })
        },
        translateEffect: function(g, h) {
            "string" == typeof g[0].style.transform && g.attr("style", "transform: translate3d(" + h + "px, 0, 0);-webkit-transform:  translate3d(" + h + "px, 0, 0);-moz-transform:  translate3d(" + h + "px, 0, 0)")
        },
        evnets: function() {
            var _param = this.params;
            var _time = _param.duration;
            var wrap_p = document.getElementById(_param.wrap_p);
            var wrap_1 = document.getElementById(_param.wrap_1);
            var wrap_2 = document.getElementById(_param.wrap_2);
            var warp_l = document.getElementsByClassName(_param.warp_l)[0];
            var _h = warp_l.clientHeight;
            if(wrap_1.length <= 0) return false;
            window.clearInterval(_param.Interval);
            _param.Interval = window.setInterval(function() {
                if ((eval(wrap_2).offsetHeight+eval(wrap_1).offsetHeight) - eval(wrap_p).scrollTop <= _h) {
                    eval(wrap_p).scrollTop -= eval(wrap_1).offsetHeight
                } else {
                    eval(wrap_p).scrollTop++
                }
            }, _time)
        }
    }

    if (typeof define === 'function' && define.amd) {
        define(function() {
            return mscrollList;
        });
    } else if (typeof module === 'object' && module.exports) {
        module.exports = mscrollList;
    } else {
        window.mscrollList = mscrollList;
    }
}).call(function() {
    return this || (typeof window !== 'undefined' ? window : global);
});